package com.dubbo.lian.cms.modules.app.mq.queue;
 import com.dubbo.lian.cms.modules.app.Checkcode;
import com.dubbo.lian.cms.modules.app.mq.Email;
import org.apache.activemq.command.ActiveMQObjectMessage;
import org.springframework.transaction.annotation.Transactional;
 import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
 public class QueueConsumer1 implements MessageListener {
     /**
     * 阿里邮箱
     * @param m
     */
    @Override
    @Transactional
    public void onMessage(Message m) {
        System.out.println();
        ActiveMQObjectMessage om = (ActiveMQObjectMessage) m;
        try {
            Email email= (Email) om.getObject();
            Checkcode checkcode = new Checkcode();
            checkcode.setPhonenumber(email.getAddress());
            checkcode.setIsuse(0);
            checkcode.setCheckcode(email.getContent());
            try {
                checkcode.insert();
            } catch (Exception e) {
                e.printStackTrace();
            }
            sendMail(email.getAddress(),email.getTitle(),email.getContent());
         } catch (JMSException e) {
            e.printStackTrace();
        }
    }
     public static void sendMail(String addressee, String title, String content) {
        Properties props = new Properties();
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.host", "smtp.mxhichina.com");
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.smtp.timeout", "180000");
        Session session = Session.getInstance(props);
         MimeMessage msg = new MimeMessage(session);
        try {
            msg.setSubject(title);
            msg.setText(content);
            msg.setFrom(new InternetAddress("lianwukun@hong-meng.com"));
            Transport transport = session.getTransport();
            transport.connect("lianwukun@hong-meng.com", "Aa5524569");
            transport.sendMessage(msg, new Address[] {new InternetAddress(addressee)});
            transport.close();
        } catch (MessagingException e) {
            e.printStackTrace();
            throw new RuntimeException("Error");
        }
    }
 }
